<?php
/**
 * Created by PhpStorm.
 * User: von liying
 * Date: 2017/11/17
 * Time: 11:36
 */
namespace backend\controllers;
/**
 * 基础控制器
 */
use common\models\UserModel;
use yii\web\Controller;

class BaseController extends Controller
{

    /**
     * @return array
     */
    public function behaviors()
    {
        return [
            'ghost-access' => [
                'class' => GhostAccessControl::className(),
            ],
            'dt-access' => [
                'class' => DtActionFilter::className(),
            ],
        ];
    }

    public function init()
    {
        $this->enableCsrfValidation = false;
    }

    public function beforeAction($action)
    {

        if (!parent::beforeAction($action)) {
            return false;

        }
        return true;
    }

    /**
     * 加锁
     * @param $model 需要加锁的模型
     * @param $userId 用户ID
     * @return array
     */
    public function locking($model, $userId)
    {
        $user = UserModel::findOne((int)$userId);
        if ($model === null || $user === null) {
            return array('state' => false, 'msg' => '数据不存在');
        }

        $model->lock_user = $userId;
        $model->lock_timestamp = date('Y-m-d H:i:s', time());
        if (!$model->save()) {
            $error = $model->getFirstErrors();
            $message = '';
            if (!empty($error)) {
                foreach ($error as $value) {
                    $message .= $value;
                }
                $tmpStr = strip_tags($message);
                $tmpLen = mb_strlen($tmpStr);
                $message = mb_substr($tmpStr, 0, $tmpLen, 'utf-8');
            }
            return array('state' => false, 'msg' => $message);
        }

        return array('state' => true, 'msg' => '加锁成功');
    }

    /**
     * 解锁
     * @param $model 需要加锁的模型
     * @param $userId 用户ID
     * @return array
     */
    public function unlocking($model, $userId)
    {
        $user = UserModel::findOne((int)$userId);
        if ($model === null || $user === null || $model->lock_user != $userId) {
            return array('state' => false, 'msg' => '数据不存在');
        }

        $model->lock_user = null;
        $model->lock_timestamp = null;
        if (!$model->save()) {
            $error = $model->getFirstErrors();
            $message = '';
            if (!empty($error)) {
                foreach ($error as $value) {
                    $message .= $value;
                }
                $tmpStr = strip_tags($message);
                $tmpLen = mb_strlen($tmpStr);
                $message = mb_substr($tmpStr, 0, $tmpLen, 'utf-8');
            }
            return array('state' => false, 'msg' => $message);
        }

        return array('state' => true, 'msg' => '解锁成功');
    }

    /**
     * 判断是否锁定
     * @param $model
     * @param $userId
     * @return array
     */
    public function is_locked($model, $userId)
    {
        if ($model === null) {
            return array('state' => false, 'msg' => '数据不存在');
        }

        if ($model->lock_user != null && $model->lock_user != $userId) {
            $user = UserModel::findOne((int)$model->lock_user);
            return array('state' => false, 'msg' => "用户{$user->realname}正在编辑");
        }

        return array('state' => true, 'msg' => '没有锁定');
    }

    /**
     * 判断多条记录是否锁定
     * @param $models findAll
     * @param $userId
     * @return array
     */
    public function more_is_locked($models, $userId)
    {
        if (empty($models)) {
            return array('state' => false, 'msg' => '数据不存在');
        }

        foreach ($models as $model) {
            if ($model->lock_user != null && $model->lock_user != $userId) {
                $user = UserModel::findOne((int)$model->lock_user);
                return array('state' => false, 'msg' => "用户{$user->realname}正在编辑");
            }
        }

        return array('state' => true, 'msg' => '没有锁定');
    }

    /**
     * 多条数据加锁
     * @param $models findAll
     * @param $userId 用户ID
     * @return array
     */
    public function more_locking($models, $userId)
    {
        $user = UserModel::findOne((int)$userId);
        if (empty($models) || $user === null) {
            return array('state' => false, 'msg' => '数据不存在');
        }

        $ids_arr = array();
        foreach ($models as $vaue) {
            array_push($ids_arr, $vaue->id);
        }

        $setData = array('lock_user' => $userId, 'lock_timestamp' => date('Y-m-d H:i:s', time()));
        $re = $models[0]::updateAll($setData, ['in', 'id', $ids_arr]);
        if (!$re) {
            return array('state' => false, 'msg' => '加锁失败');
        }

        return array('state' => true, 'msg' => '加锁成功');
    }

    /**
     * 多条数据解锁
     * @param $models findAll
     * @param $userId 用户ID
     * @return array
     */
    public function more_unlocking($models, $userId)
    {
        $user = UserModel::findOne((int)$userId);
        if (empty($models) || $user === null) {
            return array('state' => false, 'msg' => '数据不存在');
        }

        $ids_arr = array();
        $state = false;//无加锁人及加锁时间
        foreach ($models as $vaue) {
            if ($vaue->lock_user !== null || $vaue->lock_timestamp !== null) {
                $state = true;
            }
            array_push($ids_arr, $vaue->id);
        }

        if ($state) {
            $setData = array('lock_user' => null, 'lock_timestamp' => null);
            $re = $models[0]::updateAll($setData, ['in', 'id', $ids_arr]);
            if (!$re) {
                return array('state' => false, 'msg' => '解锁失败');
            }
        }

        return array('state' => true, 'msg' => '解锁成功');
    }


    /**
     * 转化为数字
     * @param $number 会计计数金额
     * @return mixed
     */
    public function set_number_format($number)
    {
        if ($number != '') {
            if (!is_numeric($number)) {
                $number = str_replace(',', '', $number);
            }
            return $number;
        }
        return 0;
    }

    /**
     * 转化为会计数字
     * @param $number
     * @return string
     */
    public function set_number_format2($number)
    {
        if ($number != '') {
            $str= 0;
            if (strrpos($number, '.')) {
                $strlen = strrpos($number, '.');
                $str1 = substr($number, $strlen);
                $str2 = number_format(substr($number, 0, $strlen));
                $str = $str2 . $str1;
            } else {
                if (is_numeric($number)) {
                    $str = number_format($number);
                }
            }
            return $str;
        }
        return 0;
    }

}

?>
